﻿//-----------------------------------------------------------------------
// <copyright file="Report.cs" company="Simnova LLC">
//     Copyright (c) Simnova LLC. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Simnova.Collaborate.Reports.Proxy
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Simnova.Collaborate.Reports.Proxy.ReportExecutionService;
    using System.IO;
    using System.Text.RegularExpressions;
    //using Simnova.Collaborate.ServiceContract.DataClasses;

    public class Report
    {
        public Report()
        {
        }

        public static byte[] ExecuteReport(string report, IList<Simnova.Collaborate.ServiceContract.DataClasses.ReportParameter> reportParameters, string reportFormat)
        {
            // Validate ReportFormat
            reportFormat = reportFormat.Trim();
            string validReportFormats = @"[HTML3.2]{7}|[HTML4.0]{7}|[MHTML]{5}|[IMAGE]{5]|[EXCEL]{5}|[WORD]{4}|[CSV]{3}|[PDF]{3}|[XML]{3}";
            Regex regex = new Regex(validReportFormats);
            if (!regex.IsMatch(reportFormat))
            {
                throw new ArgumentException("The report format provided is not a valid format", "reportFormat");
            }

            // Validate ParameterValueArray
            ParameterValue[] parameterValueArray = null;
            if (reportParameters != null && reportParameters.Count > 0)
            {
                parameterValueArray = new ParameterValue[reportParameters.Count];
                int i = 0;
                foreach (Simnova.Collaborate.ServiceContract.DataClasses.ReportParameter parameter in reportParameters)
                {
                    parameterValueArray[i] = new ParameterValue();
                    parameterValueArray[i].Name = parameter.Name;
                    parameterValueArray[i].Value = parameter.Value;
                    i++;
                }
            }

            // Initialize Variables
            byte[] result = null;
            string historyID = null;
            string deviceInfo = null; 
            TrustedUserHeader trustedUserHeader = new TrustedUserHeader();
            ServerInfoHeader serverInfoHeader = null;
            ExecutionInfo execInfo = null; 
            string extension = null;
            string mimeType = null;
            string encoding = null;
            Warning[] warnings = null;
            string[] streamIDs = null;
            ReportExecutionServiceSoapClient rs = new ReportExecutionServiceSoapClient();
         
            // Specify the user's context that all reports will run under
            rs.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
            rs.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential("ReportUser", "alcjhoG7YcAxG5GyCfG+CW");

            // Load the report information
            ExecutionHeader execHeader = rs.LoadReport(
                trustedUserHeader,
                report, 
                historyID, 
                out serverInfoHeader, 
                out execInfo);

            // Add paramenters if there are any
            if (parameterValueArray != null)
            {
                rs.SetExecutionParameters(
                    execHeader, 
                    trustedUserHeader, 
                    parameterValueArray, 
                    "en-us", 
                    out execInfo);
            }

            // Render the report
            serverInfoHeader = rs.Render(
                execHeader, 
                trustedUserHeader, 
                reportFormat, 
                deviceInfo, 
                out result, 
                out extension, 
                out mimeType, 
                out encoding, 
                out warnings, 
                out streamIDs);

            return result;
        }
    }
}
